package com.ojoin.trade.supplyloan_core.web.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.SqlId;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlSupplyInventoryDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;

@Repository
public class TbSlSupplyInventoryDaoImpl extends
		BaseDaoImpl<TbSlSupplyInventory> implements TbSlSupplyInventoryDao {

	@Override
	public List<TbSlSupplyInventory> queryByStoreDate(int platformAccountId,
			String storeDateStart, String storeDateEnd, String itemStatus,
			Pageable page) {
		return query(platformAccountId, storeDateStart, storeDateEnd, null,
				null, itemStatus, page);
	}

	@Override
	public List<TbSlSupplyInventory> queryByRowCreateTime(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String itemStatus, Pageable page) {
		return query(platformAccountId, null, null, rowCreateTimeStart,
				rowCreateTimeEnd, itemStatus, page);
	}

	@Override
	public List<TbSlSupplyInventory> query(int platformAccountId,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String storeDateStart, String storeDateEnd,
			String itemStatus, Pageable page) {

		Map<String, Object> query = new HashMap<String, Object>();
	
		// li.add(TbSlSupplyInventory.IN);
		if(!CommonUtils.isEmpty(itemStatus)){
			List<String> li = new ArrayList<String>();
			if(TbSlSupplyInventory.InAndOut.equals(itemStatus)){
				li.add(TbSlSupplyInventory.IN);
				li.add(TbSlSupplyInventory.OUT);
				
			}else {
				li.add(itemStatus);
			}
			query.put("itemStatus", li);
		}


		query.put("platformAccountId", platformAccountId + "");
		query.put("storeDateStart", storeDateStart);
		query.put("storeDateEnd", storeDateEnd);

		query.put("rowCreateTimeStart", rowCreateTimeStart);
		query.put("rowCreateTimeEnd", rowCreateTimeEnd);

		return sqlSessionTemplate.selectList(getSqlName("select"), query,
				getPageBounds(page));

	}

	@Override
	public long queryInventoryMoneyByStoreDate(int platformAccountId,
			String storeDateStart, String storeDateEnd, String itemStatus) {
		return queryInventoryMoney(platformAccountId, storeDateStart,
				storeDateEnd, null, null, itemStatus);
	}

	@Override
	public long queryInventoryMoneyByRowCreateTime(int platformAccountId,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String itemStatus) {
		return queryInventoryMoney(platformAccountId, null, null,
				rowCreateTimeStart, rowCreateTimeEnd, itemStatus);
	}
	
	@Override
	public long queryInventoryMoney(int platformAccountId,
			String storeDateStart, String storeDateEnd,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String itemStatus) {
		Map<String, Object> query = new HashMap<String, Object>();
		List<String> li = new ArrayList<String>();
		// li.add(TbSlSupplyInventory.IN);
		li.add(itemStatus);

		query.put("itemStatus", li);

		query.put("platformAccountId", platformAccountId + "");
		query.put("storeDateStart", storeDateStart);
		query.put("storeDateEnd", storeDateEnd);

		query.put("rowCreateTimeStart", rowCreateTimeStart);
		query.put("rowCreateTimeEnd", rowCreateTimeEnd);

		Object o = sqlSessionTemplate.selectOne(
				getSqlName(SqlId.SQL_SELECT_COUNT), query);

		if (null == o) {
			return 0;
		} else {
			return Long.valueOf(o.toString());
		}
	}

	@Override
	public int deleteByPlatformAccountId(Integer platformAccountId,
			String rowCreateTime) {
		try {
			Map<String, String> parames = new HashMap<String, String>();
			parames.put("platformAccountId", platformAccountId + "");
			parames.put("rowCreateTime", rowCreateTime);
			return sqlSessionTemplate.delete(
					getSqlName("deleteByPlatformAccountId"), parames);
		} catch (Exception e) {
			throw new DaoException(String.format("根据ID删除对象出错！语句：%s",
					getSqlName(SqlId.SQL_DELETE_BY_ID)), e);
		}
	}

	@Override
	public void deleteRepeat(int platformAccountId) {
		sqlSessionTemplate.delete(
				getSqlName("deleteRepeat"), platformAccountId);
		
	}

	@Override
	public String getLastInsertTime(int platformAccountId) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLastInsertTime"), platformAccountId);
	}

	@Override
	public String getLastStoreDate(int platformAccountId) {
		
		return getLastStoreDate(platformAccountId,null);
	}

	@Override
	public String getLastStoreDate(int platformAccountId, String itemStatus) {
		Map<String, Object> query = new HashMap<String, Object>();
		
		query.put("platformAccountId", platformAccountId);
		
		if(!CommonUtils.isEmpty(itemStatus)){
			List<String> li = new ArrayList<String>();
			// li.add(TbSlSupplyInventory.IN);
			li.add(itemStatus);

			query.put("itemStatus", li);
		}

		
		return sqlSessionTemplate.selectOne(getSqlName("getLastStoreDate"), query);
	}

	

}
